using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace QDCommon.Secret
{
    public class MD5Helper
    {
        public static string computHush(byte[] data)
        {
            string md5Str = "";
            using (MD5 md5 = new MD5CryptoServiceProvider())
            {
                byte[] md5strBytes =  md5.ComputeHash(data);
                md5Str =  convert(md5strBytes);
            }
           
            return md5Str;
        }

        public static string computHush(string str)
        {
            var bytes = System.Text.Encoding.UTF8.GetBytes(str);
            return computHush(bytes);
        }

        public static string computHush(Stream stream)
        {
            string md5Str = "";
            using (MD5 md5 = new MD5CryptoServiceProvider())
            {
                byte[] md5strBytes =  md5.ComputeHash(stream);
                md5Str = convert(md5strBytes);
            }
           
            return md5Str;
        }

        private static string convert(byte[] bytes)
        {
            var sb = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                byte b = bytes[i];
                sb.Append(b.ToString("x2"));
            }
            return sb.ToString();
        }
    }
}